﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.ComponentModel;

namespace EAppointmentsWPF
{
	public partial class AppointmentList
	{
        public EAppointmentsWPF.Data.Patient obj;
		public AppointmentList()
		{
			this.InitializeComponent();

			// Insert code required on object creation below this point.
		}
        #region Sorting ListView
        GridViewColumnHeader lastClickedHeader = null;
        ListSortDirection lastSortDirection = ListSortDirection.Ascending;

        void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;
            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != lastClickedHeader)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (lastSortDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    string header = headerClicked.Column.Header as string;
                    if (header != null)
                    {
                        if (header == "UBRN")
                        {
                            header = "UBRNno";
                        }
                        else if (header == "Date")
                        {
                            header = "Datevalue";
                        }
                        else if (header == "Patient Name")
                        {
                            header = "PatientName";
                        }
                        else if (header == "Referrer Name")
                        {
                            header = "ReferrerName";
                        }
                        sort(header, direction);

                        if (direction == ListSortDirection.Ascending)
                        {
                            headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowUp"] as DataTemplate;
                        }
                        else
                        {
                            headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowDown"] as DataTemplate;
                        }

                        //Remove arrow
                        if (lastClickedHeader != null && lastClickedHeader != headerClicked)
                        {
                            lastClickedHeader.Column.HeaderTemplate = Resources["ListViewColumnHeaderTemplate"] as DataTemplate;
                        }
                        lastClickedHeader = headerClicked;
                        lastSortDirection = direction;
                    }

                }
            }
        }

        void sort(string sortBy, ListSortDirection direction)
        {
            //ICollectionView dataView = CollectionViewSource.GetDefaultView(lstView.ItemsSource);

            //dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            //dataView.SortDescriptions.Add(sd);
            //dataView.Refresh();
            lstView.Items.SortDescriptions.Clear();
            lstView.Items.SortDescriptions.Add(sd);
            lstView.Items.Refresh();

        }
        #endregion

        public event EventHandler AppointmentSelected;

        private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           obj=(EAppointmentsWPF.Data.Patient)lstView.SelectedItem;
            if (AppointmentSelected != null)
                {
                    AppointmentSelected(sender, new EventArgs());
                }
                
        }
    }
}